package com.zr.kucunkufangbiaoshi.controller.service.impl;


import com.zr.kucunkufangbiaoshi.controller.mapper.DepotMapper;
import com.zr.kucunkufangbiaoshi.controller.meiju.StatusEnum;
import com.zr.kucunkufangbiaoshi.controller.pojo.*;
import com.zr.kucunkufangbiaoshi.controller.service.DepotService;
import com.zr.kucunkufangbiaoshi.controller.util.AllRecords;
import com.zr.kucunkufangbiaoshi.controller.util.ResultBuildVo;
import com.zr.kucunkufangbiaoshi.controller.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * Created by Administrator on 2019/7/16 0016.
 */
@Service
public class DepotServiceImpl implements DepotService {
    @Autowired
    private DepotMapper depotMapper;
    @Override
    public ResultVo<AllRecords> queryPageDepot(DepotSelectVo depotSelectVo) {
        //查询数据
        List<Depot> depotList = depotMapper.queryPageDepot(depotSelectVo);
        //查询数量
        int count = depotMapper.queryCountDepot(depotSelectVo);
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(depotSelectVo.getPageIndex());
        allRecords.setPageSize(depotSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        allRecords.setDataList(depotList);
        return ResultBuildVo.success(allRecords);
    }

    @Override
    public ResultVo updateDepotStatus(DepotUpdateStatus depotUpdateStatus) {
        Depot depot = new Depot();
        Date nowDate = new Date();
        depot.setUpdateTime(nowDate);
        depot.setUpdateName("小花话");
        BeanUtils.copyProperties(depotUpdateStatus, depot);
        depotMapper.updateDepotStatus(depot);
        return ResultBuildVo.success(depotUpdateStatus);


    }

    @Override
    @Transactional
    public ResultVo updateDepot(DepotUpdateVo depotUpdateVo) {
        //判断是否为禁用，禁用可以修改
        ResultVo assigment1 = assigmentUpdate1(depotUpdateVo);
        if (!assigment1.isSuccess()) {
            return assigment1;
        }
        //给数据库中的创建时间和修改时间等信息赋值
        Depot depot = new Depot();
        Date nowDate = new Date();
        depot.setUpdateTime(nowDate);
        depot.setUpdateName("花花");
        BeanUtils.copyProperties(depotUpdateVo, depot);
        depotMapper.updateDepot(depot);
        return ResultBuildVo.success(depotUpdateVo);
    }

    @Override
    @Transactional
    public ResultVo addDepot(DepotAddVo depotAddVo) {
        Depot depot = new Depot();
        Date nowDate = new Date();

        depot.setCreateTime(nowDate);
        depot.setCreateName("小明");
        depot.setUpdateTime(nowDate);
        depot.setUpdateName("小明");
        BeanUtils.copyProperties(depotAddVo,depot);
        depotMapper.addDepot(depot);
        return ResultBuildVo.success(depotAddVo);

    }

    private ResultVo assigmentUpdate1(DepotUpdateVo depotUpdateVo) {
        Depot depot = depotMapper.queryDepotById(depotUpdateVo.getId());
        if (depot == null){
            return ResultBuildVo.error("修改数据不存在！","500");
        }
        //禁用状态下才允许修改
        if (depot.getStatus() == StatusEnum.QIYONG.getStatusValue()){
            return ResultBuildVo.error("禁用状态才能修改！","500");
        }
        return ResultBuildVo.success(depotUpdateVo);
    }
}
